<?php
/**
 * Created by PhpStorm.
 * User: jack.shan
 * Date: 2020/9/15
 * Time: 16:57
 */
namespace app\controller;

use app\BaseController;
use app\model\User;
use app\validate\UserLo;
use app\validate\Userv;
use app\common\Verify;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{

    public function index()
    {
        return 'this is test';
    }

    public function hello2()
    {
        return 'this is hello';
//        $name = $this->request->param('name');
//        return "hello " . $name;
    }

    public function register()
    {
        if($this->request->isPost())
        {
            $name = $this->request->param('name');
            $password = $this->request->param('password');
            $email = $this->request->param('email');

            validate(Userv::class)->check([
                'username'  => $name,
                'password' => $password,
                'email' => $email,
            ]);

            $user = User::where('email',$email)->find();

            if($user)
            {
                return "该邮箱已注册";
            }

            $user = new User();
            $user->username = $name;
            $user->password = md5($password);
            $user->email = $email;
            $user->fans = 0;
            $r = $user->save();

            if($r)
            {
                return "注册成功";
            }
            else
            {
                return "注册失败";
            }
        }

        return View::fetch('register');
    }


    public function login()
    {
        if($this->request->isPost())
        {
            $name = $this->request->param('name');
            $password = $this->request->param('password');

            validate(UserLo::class)->check([
                'username'  => $name,
                'password' => $password,
            ]);

            $user = User::where('username',$name)->find();
            if(empty($user))
            {
                return redirect('/login');
            }
            if($user['password'] != md5($password))
            {
                return  redirect('/login');
            }

            return $user->toJson();

        }

        return View::fetch('login');
    }


    public function yzm()
    {
        $config =    array(    'fontSize'    =>    40,// 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new Verify($config);
        $Verify->entry('loginYzm');
    }

}